|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const fs = require('fs') |
|
4
|
|
|
const inquirer = require('inquirer') |
|
5
|
|
|
const util = require('../util') |
|
6
|
|
|
const output = require('../output') |
|
7
|
|
|
const Kits = require('../kits/Kits') |
|
8
|
|
|
const Kit = require('../kits/Kit') |
|
9
|
|
|
|
|
10
|
|
|
module.exports = function (kit, slug) { |
|
11
|
|
|
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt')) |
|
12
|
|
|
var questions = [] |
|
13
|
|
|
|
|
14
|
|
|
if (!kit && !slug && fs.existsSync('module.json')) { |
|
15
|
|
|
output.warn('Are you sure you want to create a new module here? There already seems to be one in ' + |
|
16
|
|
|
'this directory. Instead, did you mean to type `inc run`?\n') |
|
17
|
|
|
} else { |
|
18
|
|
|
output.newline() |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
if (!slug || !slug.length) { |
|
22
|
|
|
var userName = (util.getSystemUsername() || 'user').toLowerCase() |
|
23
|
|
|
questions.push({ |
|
24
|
|
|
type: 'input', |
|
25
|
|
|
name: 'slug', |
|
26
|
|
|
default: userName + '/my-module', |
|
27
|
|
|
message: 'What should we name this module?', |
|
28
|
|
|
validate: function (value) { |
|
29
|
|
|
if (value.match(/^([a-z0-9-]+\/)?[a-z0-9-]+$/)) { |
|
30
|
|
|
return true |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return 'Module name should only contain lowercase letters, numbers and dashes.' |
|
34
|
|
|
} |
|
35
|
|
|
}) |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!kit || !kit.length) { |
|
39
|
|
|
questions.push({ |
|
40
|
|
|
type: 'autocomplete', |
|
41
|
|
|
name: 'kit', |
|
42
|
|
|
message: 'What template would you like to use?', |
|
43
|
|
|
source: function (answers, input) { |
|
44
|
|
|
return new Promise(function (resolve) { |
|
45
|
|
|
Kits.list(function (list) { |
|
46
|
|
|
resolve(input && input.length ? list.filter(function (value) { |
|
47
|
|
|
return value.indexOf(input) >= 0 |
|
48
|
|
|
}) : list) |
|
49
|
|
|
}) |
|
50
|
|
|
}) |
|
51
|
|
|
}, |
|
52
|
|
|
default: 'basic' |
|
53
|
|
|
}) |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Prompt questions |
|
57
|
|
|
inquirer.prompt(questions).then(function (answers) { |
|
58
|
|
|
output.newline() |
|
59
|
|
|
Kit.create(answers.kit || kit, answers.slug || slug) |
|
60
|
|
|
}) |
|
61
|
|
|
} |
|
62
|
|
|
|